//
//  SNRepairTypeView.m
//  app
//
//  Created by 王少宁 on 15/9/1.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNRepairTypeView.h"
#import "SNRepairTypeModel.h"

@interface SNRepairTypeView ()
@property (nonatomic, retain) NSArray * repairTypeArray;
@property (nonatomic, retain) UIButton * repairTypeBtn;
@end

@implementation SNRepairTypeView

- (instancetype)initWithFrame:(CGRect)frame repairTypeArray:(NSArray*)repairTypeArray repairTypeBtn:(UIButton *)repairTypeBtn
{
    if ([super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenHeight)]) {
        self.hidden = YES;
        UIView * backGoundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenHeight)];
        backGoundView.backgroundColor = [UIColor blackColor];
        backGoundView.alpha = 0.4;
        [self addSubview:backGoundView];
        self.repairTypeArray = repairTypeArray;
        self.repairTypeBtn = repairTypeBtn;
        [self createViewWithFrame:frame];
    }
    return self;
}

- (void)createViewWithFrame:(CGRect)frame;
{
    UIScrollView * btnBackGroundView = [[UIScrollView alloc] initWithFrame:frame];
    btnBackGroundView.backgroundColor = [XCommon hexStringToColor:kColor_l_light_gray_background];
    btnBackGroundView.contentSize = CGSizeMake(frame.size.width, kRepairTypeSubBtnH * self.repairTypeArray.count + UP(self.repairTypeArray.count - 1));
    for (int i = 0; i < self.repairTypeArray.count; i++) {
        UIButton * repairTypeSubBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        repairTypeSubBtn.frame = CGRectMake(0, (kRepairTypeSubBtnH + UP(1)) * i, frame.size.width, kRepairTypeSubBtnH);
        repairTypeSubBtn.backgroundColor = [XCommon hexStringToColor:kColor_lightGray_cell];
        repairTypeSubBtn.titleLabel.font = JKAppGlobleFontSmallTagText;
        SNRepairTypeModel * repairTypeModel = self.repairTypeArray[i];
        [repairTypeSubBtn setTitle:repairTypeModel.option_name forState:UIControlStateNormal];
        [repairTypeSubBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        repairTypeSubBtn.tag = i+1;
        [repairTypeSubBtn addTarget:self action:@selector(repairTypeSubBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btnBackGroundView addSubview:repairTypeSubBtn];
    }
    [self addSubview:btnBackGroundView];
}

- (void)repairTypeSubBtnClick:(UIButton *)btn
{
    SNRepairTypeModel * repairTypeModel = self.repairTypeArray[btn.tag - 1];
    [self.repairTypeBtn setTitle:repairTypeModel.option_name forState:UIControlStateNormal];
    self.repairTypeBtn.tag = kRepairTypeSubBtnBaseTag + repairTypeModel.option_value.intValue;
    self.repairTypeBtn.titleLabel.text = [NSString stringWithFormat:@"%@",repairTypeModel.com_id];
    self.repairTypeBtn.titleLabel.tag = repairTypeModel.root_id.integerValue;
    self.hidden = YES;
}

- (void)show
{
    self.hidden = NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.hidden = YES;
}

@end
